package main.extrates;

import android.app.Activity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.TextView;
import java.io.InputStream;
import java.net.URL;
import java.util.*;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserFactory;
import java.net.URLConnection;



public class ExtRates_GraphPainterActivity extends Activity 
{
	private Spinner PeriodSpinner;
	protected Spinner DaySpinner;
	protected Spinner MonthSpinner;
	protected Spinner YearSpinner;
	private Spinner CurrencySpinner;
	private ImageView Graph;
	private TextView Status;
	private ExtRates_GraphPainterPainter Graphic;
	protected ArrayList<String> Files;
	
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        PeriodSpinner = (Spinner)findViewById(R.id.Period);
        DaySpinner = (Spinner)findViewById(R.id.Day);
        MonthSpinner = (Spinner)findViewById(R.id.Month);
        YearSpinner = (Spinner)findViewById(R.id.Year);
        CurrencySpinner = (Spinner)findViewById(R.id.Currency);
        Graph = (ImageView)findViewById(R.id.Graph);
        Status = (TextView)findViewById(R.id.Status);
        
        SetPeriodSpinner();
        SetCurrencySpinner();
        SetDaySpinner();
        SetMonthSpinner();
        SetYearSpinner();

        PeriodSpinner.setOnItemSelectedListener(new ExtRates_GraphPainterMyOnItemSelectedListener(this));
        DaySpinner.setOnItemSelectedListener(new ExtRates_GraphPainterMyOnItemSelectedListener(this));
        MonthSpinner.setOnItemSelectedListener(new ExtRates_GraphPainterMyOnItemSelectedListener(this));
        YearSpinner.setOnItemSelectedListener(new ExtRates_GraphPainterMyOnItemSelectedListener(this));
        CurrencySpinner.setOnItemSelectedListener(new ExtRates_GraphPainterMyOnItemSelectedListener(this));
    }
    
    private void SetPeriodSpinner()
    {
        List<String> PeriodArray = new ArrayList<String>();
        PeriodArray.add("Day");
        PeriodArray.add("Week");
        PeriodArray.add("Month");
        PeriodArray.add("Year");

        ArrayAdapter<String> aspnMusketeers =  new ArrayAdapter<String>(this, R.layout.row, R.id.SpinnerPopupText, PeriodArray);
        PeriodSpinner.setAdapter(aspnMusketeers);
    }
    
    private void SetCurrencySpinner()
    {
        List<String> CurrencyArray = new ArrayList<String>();
        CurrencyArray.add("EUR-USD");
        CurrencyArray.add("GBP-USD");
        CurrencyArray.add("USD-CHF");
        CurrencyArray.add("USD-SEK");
        CurrencyArray.add("USD-DKK");
        CurrencyArray.add("USD-NOK");
        CurrencyArray.add("USD-CZK");
        CurrencyArray.add("USD-SKK");
        CurrencyArray.add("USD-PLN");
        CurrencyArray.add("USD-HUF");
        CurrencyArray.add("USD-RUB");
        CurrencyArray.add("USD-TRY");
        CurrencyArray.add("USD-ILS");
        CurrencyArray.add("USD-KES");
        CurrencyArray.add("USD-ZAR");
        CurrencyArray.add("USD-MAD");
        ArrayAdapter<String> aspnMusketeers1 =  new ArrayAdapter<String>(this, R.layout.row, R.id.SpinnerPopupText, CurrencyArray);
        CurrencySpinner.setAdapter(aspnMusketeers1);
    }
    
    private void SetDaySpinner()
    {
    	String Temp;
        List<String> DayArray = new ArrayList<String>();
        
        for (int i = 1; i <= 31; i++)
        {
        	if (i < 10)
        		Temp = "0" + Integer.toString(i);
        	else
        		Temp = Integer.toString(i);
        	
        	DayArray.add(Temp);
        }

        ArrayAdapter<String> aspnMusketeers =  new ArrayAdapter<String>(this, R.layout.row, R.id.SpinnerPopupText, DayArray);
        DaySpinner.setAdapter(aspnMusketeers);
    }
    
    private void SetMonthSpinner()
    {
    	String Temp;
        List<String> MonthArray = new ArrayList<String>();
        
        for (int i = 1; i <= 12; i++)
        {
        	if (i < 10)
        		Temp = "0" + Integer.toString(i);
        	else
        		Temp = Integer.toString(i);
        	
        	MonthArray.add(Temp);
        }

        ArrayAdapter<String> aspnMusketeers =  new ArrayAdapter<String>(this, R.layout.row, R.id.SpinnerPopupText, MonthArray);
        MonthSpinner.setAdapter(aspnMusketeers);
    }
    
    private void SetYearSpinner()
    {
        List<String> YearArray = new ArrayList<String>();
        
        for (int i = 2010; i <= 2015; i++)
        	YearArray.add(Integer.toString(i));

        ArrayAdapter<String> aspnMusketeers =  new ArrayAdapter<String>(this, R.layout.row, R.id.SpinnerPopupText, YearArray);
        YearSpinner.setAdapter(aspnMusketeers);
    }
    
	public void Show()
    {
		String FileName = "http://10.0.2.2:34644/GetFromDBService.svc/Chart" + PeriodSpinner.getSelectedItem().toString() + "/" + 
				DaySpinner.getSelectedItem().toString() + "-" + MonthSpinner.getSelectedItem().toString() + "-" + 
				YearSpinner.getSelectedItem().toString() + "/" + CurrencySpinner.getSelectedItem().toString();

		ArrayList<Double> Values = ParseXML(FileName);
		Graphic = new ExtRates_GraphPainterPainter();
        Graphic.Paint(Graph, Values);

    }
	
	private ArrayList<Double> ParseXML(String FileName)
	{
		ArrayList<Double> Values = new ArrayList<Double>();

        try
        {
        	XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
        	factory.setNamespaceAware(true);
        	XmlPullParser parser = factory.newPullParser();

        	URL url = new URL(FileName);
        	URLConnection urlConn = url.openConnection();
        	urlConn.setConnectTimeout(3000);
        	InputStream IS = urlConn.getInputStream();
        	
        	try
            {
                parser.setInput(IS, null);
                int eventType = parser.getEventType();
                while (eventType != XmlPullParser.END_DOCUMENT) 
                {
                	if (eventType == XmlPullParser.START_TAG) 
                		if (parser.getName().equals("Rate"))
                		{
                			eventType = parser.next();
            				eventType = parser.next();
            				Values.add(Double.valueOf(parser.getText().trim()).doubleValue());
                		}
                	eventType = parser.next();
                }
            }
            finally 
            {
            	IS.close();
            	Status.setText("");
            }
        }
        catch (Throwable e)
        {
            Status.setText("Error. " + e.toString());
        }
        
		return Values;
	}
    
}